#include <gtk/gtk.h>

#include "myapp.h"

// menyimpan userinfo ke .myspeedy
void save_mykeys (GKeyFile *key, userinfo *uri)
{
	gchar config_path[256];
	gchar *data;
	gsize sz;

	if (!uri->alerturl[0])
		g_strlcpy (uri->alerturl, DEF_ALERTURL, 128);

	g_key_file_set_string (key, "telkom", "url", uri->alerturl);
	g_key_file_set_string (key, "telkom", "speedy", uri->speedynumber);
	g_key_file_set_integer (key, "application","minutes", uri->checkinterval);
	g_key_file_set_integer (key, "application","alert", uri->limit2alert);

	data = g_key_file_to_data (key, &sz, NULL);

	g_strlcpy(config_path,g_get_home_dir(),256);
	g_strlcat (config_path, CONFIG_FILENAME, 256);
	g_file_set_contents (config_path, data, sz, NULL);
	g_free(data);
}

// membaca .myspeedy
void read_mykeys (userinfo *uri)
{
	gchar config_path[256];
	gchar *tempstr;
	GKeyFile *key;
	int err = 1;

	g_strlcpy (uri->speedynumber,"",16);
	g_strlcpy (uri->alerturl, DEF_ALERTURL, 128);
	uri->checkinterval = 20;
	uri->limit2alert = 50;

	g_strlcpy (config_path, g_get_home_dir(),256);
	g_strlcat (config_path, CONFIG_FILENAME, 256);

	key = g_key_file_new ();
	if (g_key_file_load_from_file (key, config_path, 0, NULL))
	{
		err = 0;
		if ( (tempstr = g_key_file_get_string (key, "telkom", "speedy", NULL)) != NULL)
		{
			g_strlcpy (uri->speedynumber, tempstr, 16);
			g_free(tempstr);
		}
		else
			err = 1;

		if ( (tempstr = g_key_file_get_string (key, "telkom", "url", NULL)) != NULL)
		{
			g_strlcpy (uri->alerturl, tempstr, 128);
			g_free(tempstr);
		}
		else
			err = 1;

		 uri->checkinterval = g_key_file_get_integer (key, "application", "minutes", NULL);
		 uri->limit2alert = g_key_file_get_integer (key, "application", "alert", NULL);
	}

	if (err)
	{
		save_mykeys (key, uri);
	}
	g_key_file_free (key);
}
